package com.bdqn.controller;

import com.bdqn.Service.Orders.OrdersService;
import com.bdqn.Service.QuoteDetail.QuoteDetailSevice;
import com.bdqn.Service.Supplier.SupplierService;
import com.bdqn.entity.Employee;
import com.bdqn.entity.Orders;
import com.bdqn.entity.QuoteDetail;
import com.bdqn.entity.Supplier;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ResponseBody
@RequestMapping("/Quote")
public class QuoteDetailController {
    @Autowired
    SupplierService supplierService;
    @Autowired
    QuoteDetailSevice quoteDetailSevice;
    @Autowired
    OrdersService ordersService;
    /**
     * 修改
     */
    @RequestMapping("/update")
    public boolean Update(QuoteDetail quoteDetail){
        Subject subject= SecurityUtils.getSubject();
        Employee employee = (Employee) subject.getSession().getAttribute("employee");//员工
        Supplier supplier = supplierService.selectSupplierById(employee.getId());
        quoteDetail.setSupplierId(supplier.getId());
        quoteDetail.setTotalPrice((Integer.parseInt(quoteDetail.getAmount())*quoteDetail.getUnitPrice())+quoteDetail.getOtherPrice()+quoteDetail.getMixPrice());
        quoteDetailSevice.Update(quoteDetail);
        Orders orders=new Orders();
        orders.setId(quoteDetail.getOrderId());
        orders.setStatus("已报价");
        ordersService.Update1(orders);
        return true;
    }
    /**
     * 根据id查
     */
    @RequestMapping("/SelById")
    public QuoteDetail SelById(@Param("id") int id){
        QuoteDetail quoteDetail=new QuoteDetail();
        quoteDetail.setOrderId(id);
        Subject subject= SecurityUtils.getSubject();
        Employee employee = (Employee) subject.getSession().getAttribute("employee");//员工
        Supplier supplier = supplierService.selectSupplierById(employee.getId());
        quoteDetail.setSupplierId(supplier.getId());
        QuoteDetail quoteDetail1 = quoteDetailSevice.SelById(quoteDetail);
        return quoteDetail1;
    }
}
